<?php

class Application_Model_Dao_Semester extends Zend_Db_Table_Abstract
{

	protected $_name = 'semesters';
	protected $_primary = 'id';

	public function init(){
		 
	}
	public function getAll(){
		$result = $this->fetchAll();
		if( !$result )
		throw new Exception("Wystąpił błąd przy pobieraniu danych z bazy. Proszę spróbować jeszcze raz. Jeśli problem będzie się powtarzać, prosimy skontaktować się z administratorem.");
		return $result;
	}
public function getAllSemester(){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('semesters');
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}
public function getRowSemester($id){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('semesters')->where('semesters.id = \''.$id.'\'');
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}
	public function getSemesterByID($id){
		$id = (int)$id;
		$row = $this->fetchRow('id = '.$id);
		if (!$row)
		throw new Exception("Semestr o ID $id nie istnieje.");
		return $row->toArray();
	}
	public function getSemesterByDesignation($designation){
		$row = $this->fetchRow('designation = '.$designation);
		if (!$row)
		throw new Exception("Semestr o nazwie $designation nie istnieje.");
		return $row->toArray();
	}
	public function saveSemester($semester) {
		$data = array(
			'designation' => $semester->getDesignation(),
		);
		$result = $this->insert($data);
		return $result;
	}
	public function updateSemester($semester) {
		$data = array(
			'designation' => $semester->getDesignation(),
		);
		$result = $this->update($data, 'id = '. (int)$semester->getId());
		return $result;
	}
public function deleteSemester($semester){
		$result = $this->delete('id =' . (int)$semester->getId());
		return $result;
	}
	public function getToSelect(){
		$array = $this->getAll()->toArray();
		$semesters[''] = "wybierz";
		foreach ($array as $option) {
			$semesters[ $option['id'] ] = $option['designation' ] ;
		}
		return $semesters;
	}

}
 